// Author: TOKUMOTO Susumu
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;

using EnvDTE;
using EnvDTE80;


namespace EEst.SoftwareFactory.Actions.lib
{
    class PSFActionUtility
    {
        static public string FindProjectAbsolutePath(Solution2 currentSolution, Project targetProject)
        {
            string path = FindTargetProject(currentSolution, targetProject);
            string solutionPath = (string)currentSolution.Properties.Item("Path").Value;
            string solutionDir = Path.GetDirectoryName(solutionPath);

            return solutionDir + Path.DirectorySeparatorChar + path;
        }
        static public string FindTargetProject(Solution2 current, Project target)
        {
            string result = null;
            foreach (Project project in current.Projects)
            {
                if (project.Equals(target))
                {
                    return project.Name;
                }
                result = FindSubProject(project, target, project.Name);
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }
        static private string FindSubProject(Project current, Project target, string path)
        {
            if (current.Equals(target))
            {
                return path + Path.DirectorySeparatorChar + current.Name;
            }
            string result = null;
            if (current.Object is SolutionFolder)
            {
                foreach (ProjectItem pi in current.ProjectItems)
                {
                    if (pi.Object is Project)
                    {
                        result = FindSubProject((Project)pi.Object, target, path);
                        if (result != null)
                        {
                            break;
                        }
                    }
                }
            }
            return result;
        }
    }
}
